package com.itheima.service;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @作者 itcast
 * @创建日期 2020/10/15 10:45
 **/
@Service
@Transactional
public class UserService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserMapper userMapper;
    public List<User> findAll(){
        List<User> users = (List<User>)redisTemplate.boundValueOps("user_list_cache").get();
        if(users == null || users.isEmpty()){
            users = userMapper.findAll();
            redisTemplate.boundValueOps("user_list_cache").set(users);
        }
        return users;
    }
}
